﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for EventHandler
    /// </summary>
    public class core_EventHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Event     
            string add_event = context.Request["add_event"];
            if (!string.IsNullOrEmpty(add_event))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_event>(add_event);
                context.Response.Write("Thực thi thành công !");         
            }
            string view_event = context.Request["view_event"];
            if (!string.IsNullOrEmpty(view_event))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_event = context.Request["list_event"];
            if (!string.IsNullOrEmpty(list_event))
            {
                RenderList(context);
            }
            string delete_event = context.Request["delete_event"];
            if (!string.IsNullOrEmpty(delete_event))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.Delete<core_event>(delete_event);
                RenderList(context);
            }
            string lockunlock_event = context.Request["lockunlock_event"];
            if (!string.IsNullOrEmpty(lockunlock_event))
            {
               BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
               bussiness.LockUnLock<core_event>(lockunlock_event, "event_active");
                RenderList(context);
            }
            #endregion
        }
        private void RenderSingle(HttpContext context,int Id)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            object obj = bussiness.Get<core_event>(
               c => c.event_id.Equals(Id),
               c => new
               {
                   id = c.event_id,
                   title = c.event_title,
                   timestart=c.time_start,
                   contentstart = c.content_start,
                   timeend=c.time_end,
                   contentend=c.content_end,
                   active = c.event_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            IList<object> list = bussiness.List<core_event>(
                null,
                null,
                null,
                null,
                 c => new
                 {
                     id = c.event_id,
                     title = c.event_title,
                     timestart = c.time_start,
                     contentstart = c.content_start,
                     timeend = c.time_end,
                     contentend = c.content_end,
                     active = c.event_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}